package com.budwk.app.sqfw.services.datamange.impl;

import com.budwk.app.sqfw.models.Sqfw_datamange_community;
import com.budwk.app.sqfw.models.Sqfw_datamange_district;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.services.datamange.DataMangeCommunityService;
import com.budwk.app.sqfw.services.datamange.DataMangeDistrictService;
import com.budwk.app.sqfw.vo.SqfwDataMangeCheckVO;
import com.budwk.app.sqfw.vo.SqfwDataMangeVO;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.adminUtils.CommonCheckUtils;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@IocBean(args = {"refer:dao"})
public class DataMangeDistrictServiceImpl extends BaseServiceImpl<Sqfw_datamange_district> implements DataMangeDistrictService {
    public DataMangeDistrictServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private SysUserService sysUserService;

    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @Inject
    private DataMangeCommunityService dataMangeCommunityService;

    @Override
    public Pagination page(SqfwDataMangeVO bean) {
        if (null == bean) {
            bean = new SqfwDataMangeVO();
        }
        String type = bean.getType();
        long startTime = System.currentTimeMillis();
        // 构造查询条件
        List<Sqfw_datamange_district> sqfwFormSummaryOnlines = new ArrayList<Sqfw_datamange_district>();
        // 查询header头
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_datamange_district::getType, "=", type);
        cnd.and(Sqfw_datamange_district::getExcelSeg, "=", "header");
        List<Sqfw_datamange_district> sqfwFormSummaryOnlines1 = query(cnd);
        if (sqfwFormSummaryOnlines1.size() <= 0) {
            return new Pagination();
        }
        Sqfw_datamange_district sqfwFormSummaryOnline = sqfwFormSummaryOnlines1.get(0);
        sqfwFormSummaryOnlines.add(sqfwFormSummaryOnline);
        Criteria sql = buildQuery(bean);
        Pagination pagination = this.listPage(bean.getPageNo(), bean.getPageSize(), sql);
        List<Sqfw_datamange_district> sqfwOnlines = pagination.getList(Sqfw_datamange_district.class);
        sqfwOnlines.forEach(sqfwFormSummaryOnline1 -> {
            sqfwFormSummaryOnlines.add(sqfwFormSummaryOnline1);
        });
        pagination.setList(sqfwFormSummaryOnlines);
        log.info("DataMangeCommunityServiceImpl-list-耗时: {}ms", System.currentTimeMillis() - startTime);
        return pagination;
    }

    @Override
    public Sqfw_datamange_district detail(SqfwDataMangeVO bean) {
        Criteria sql = buildQuery(bean);
        return this.fetch(sql);
    }

    /**
     * 构造查询条件
     *
     * @param bean
     * @return
     */
    private Criteria buildQueryCond(SqfwDataMangeVO bean, Sqfw_datamange_district sqfwDatamangeDistrict) {
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = cri.where();
        // 不查询excel头
        criSql.andNotEquals(Sqfw_datamange_community::getExcelSeg, "header");
        criSql.andEquals(Sqfw_datamange_community::getDelFlag, false);
        if (null != bean && StringUtils.isNotEmpty(bean.getPlaceType())) {
            criSql.andLike("param_data->>'placeType'", bean.getPlaceType());
        }
        if (null != bean && StringUtils.isNotEmpty(bean.getCommunity())) {
            criSql.andLike(Sqfw_datamange_community::getCommunity, bean.getCommunity());
        }
        return cri;
    }

    private Criteria buildQuery(SqfwDataMangeVO bean) {
        Criteria cri = Cnd.cri();
        SqlExpressionGroup criSql = cri.where();
        // 区域5级检查
        CommonCheckUtils.checkAreaRange(criSql);

        // 不查询excel头
        criSql.andNotEquals(Sqfw_datamange_community::getExcelSeg, "header");
        criSql.andEquals(Sqfw_datamange_community::getDelFlag, false);

        if (null != bean && StringUtils.isNotEmpty(bean.getQueryCondition())) {
            SqlExpressionGroup placeName = Cnd.exps("excel_data->>'placeName'", "like", "%" + bean.getQueryCondition() + "%");
            SqlExpressionGroup businessLicenseName = Cnd.exps("excel_data->>'businessLicenseName'", "like", "%" + bean.getQueryCondition() + "%");
            SqlExpressionGroup socialCreditCode = Cnd.exps("excel_data->>'socialCreditCode'", "like", "%" + bean.getQueryCondition() + "%");
            SqlExpressionGroup location = Cnd.exps("excel_data->>'location'", "like", "%" + bean.getQueryCondition() + "%");
            SqlExpressionGroup address = Cnd.exps("excel_data->>'address'", "like", "%" + bean.getQueryCondition() + "%");
            SqlExpressionGroup operator = Cnd.exps("excel_data->>'operator'", "like", "%" + bean.getQueryCondition() + "%");
            SqlExpressionGroup operatorPhone = Cnd.exps("excel_data->>'operatorPhone'", "like", "%" + bean.getQueryCondition() + "%");
            SqlExpressionGroup placeArea = Cnd.exps("excel_data->>'placeArea'", "like", "%" + bean.getQueryCondition() + "%");
            criSql.and(placeName.or(businessLicenseName).or(socialCreditCode).or(location).or(address).or(operator).or(operatorPhone).or(placeArea));
        }
        // id
        if(null != bean && StringUtils.isNotEmpty(bean.getId())) {
            criSql.and(SqfwDataMangeVO::getId, "=", bean.getId());
        }
        // 类型
        if(null != bean && StringUtils.isNotEmpty(bean.getType())) {
            criSql.and(SqfwDataMangeVO::getType, "=", bean.getType());
        }
        // summaryId
        if(null != bean && StringUtils.isNotEmpty(bean.getSqfwSummaryId())) {
            criSql.and(SqfwDataMangeVO::getSqfwSummaryId, "=", bean.getSqfwSummaryId());
        }
        // 场所名称
        if(null != bean && StringUtils.isNotEmpty(bean.getPlaceName())) {
            criSql.and(SqfwDataMangeVO::getPlaceName, "=", bean.getPlaceName());
        }
        return cri;
    }
}
